package com.hp.gui.jtree;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO 注释
 *
 * @author hupan
 * @date 2019-11-20 14:15
 */
public class DocsTreeTransferHanlder extends TransferHandler {
    public DocsTreeTransferHanlder() throws ClassNotFoundException {
    }

    @Override
    public int getSourceActions(JComponent c) {
        return MOVE;
    }

    @Override
    protected Transferable createTransferable(JComponent c) {
        JTree tree = (JTree) c;
        TreePath[] paths = tree.getSelectionPaths();
        ArrayList nodes = new ArrayList();
        for (TreePath path : paths) {
            nodes.add(path.getLastPathComponent());
        }
        return new JTreeTransferable(nodes);
    }

    @Override
    protected void exportDone(JComponent source, Transferable data, int action) {
    }

    @Override
    public boolean canImport(TransferSupport support) {
        if (support.isDataFlavorSupported(JTreeTransferable.FLAVOR)) {
            if (support.getDropAction() == MOVE) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean importData(TransferSupport support) {
        JTree tree = (JTree) support.getComponent();
        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
        Transferable transfer = support.getTransferable();
        try {
            List<DefaultMutableTreeNode> nodes = (List<DefaultMutableTreeNode>) transfer.getTransferData(JTreeTransferable.FLAVOR);
            JTree.DropLocation location = (JTree.DropLocation) support.getDropLocation();
            DefaultMutableTreeNode newParent = (DefaultMutableTreeNode) location.getPath().getLastPathComponent();
            for (DefaultMutableTreeNode node : nodes) {
                model.removeNodeFromParent(node);
                model.insertNodeInto(node, newParent, newParent.getChildCount());
            }
        } catch (UnsupportedFlavorException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}

class JTreeTransferable implements Transferable {
    public static DataFlavor FLAVOR = null;
    private List<DefaultMutableTreeNode> nodes;

    JTreeTransferable(ArrayList<DefaultMutableTreeNode> nodes) {
        try {
            FLAVOR = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + ";class=\"" + ArrayList.class.getName() + "\"");
            this.nodes = nodes;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
        return nodes;
    }

    @Override
    public DataFlavor[] getTransferDataFlavors() {
        return new DataFlavor[]{FLAVOR};
    }

    @Override
    public boolean isDataFlavorSupported(DataFlavor flv) {
        return FLAVOR.equals(flv);
    }
}